<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-0531
*/

/**
 * 
 * 用户控制器
 * @author xjh
 *
 */
class UserController extends Controller{

	//访问控制
	function filters(){
		return array(
			'accessControl'
		);
	}
	
	
	function accessRules(){
		
		return array(  
	  /*      array('allow',  // 所有用户有操作权限：index,view  
	            'actions'=>array('index','view'),  
	            'users'=>array('*'),  
	        ),  
	        array('allow', // 仅登录用户有权限操作：create,update  
	            'actions'=>array('create','update'),  
	            'users'=>array('@'),  
	        ),  
	        array('allow', // 指定用户有权限操作：admin,delete  
	            'actions'=>array('admin','delete'),  
	            'users'=>array('admin'),  
	        ),  
	        array('deny',  // 禁止所有用户操作  
	            'users'=>array('*'),  
	        ),  */
		  array('allow', // 仅登录用户有权限操作：create,update  
	            'actions'=>array('create','update'),  
	            'users'=>array('@'),  
	        ),  
	    );  
		
	}
	
	
	function actionUserTop(){
		$this->renderPartial('userTop');
	}
	
	
	/**
	 * 加载用户信息
	 */
	function actionUser(){
		$user = Useradmin::model();
		$infos = $user -> getAllUserInfos();
		$this->renderPartial('userList',array('user_info'=>$infos));
	}
	
	function actionuseradd(){
		
		$user = Useradmin::model();
		$role_infos = $user->getRoleInfo();
		$this->renderPartial('useradd',array('role_infos'=>$role_infos));
	}
	
	
	function actionAdduser(){	
		$_POST['user_password'] = sha1($_POST['user_password']);//加密处理
		$user = new Useradmin();
		$result = $user -> adduser();
		if($result){
			echo "<script>alert('添加成功');</script>";
		}else{
			echo "<script>alert('添加失败');</script>";
		}
		$infos = $user -> getAllUserInfos();
		$this->renderPartial('userTop');
	}
	/**
	 * @author xjh
	 * 修改
	 */
	function actionUpdate($user_id){
		
		$user = Useradmin::model();
		$one_userinfo = $user->getUserInfoById($user_id);
		$one_userinfo = $one_userinfo['0'];
		$role_infos = $user->getRoleInfo();
		
		if($_POST){
			if(!empty($_POST['user_password'])) $_POST['user_password'] = sha1($_POST['user_password']);
			$row = $user -> updateUser($user_id);
			if($row){
				echo "<script>alert('修改成功');</script>";
		$this->renderPartial('userTop');
			}else{
				echo "修改失败";
			}
		}else{
			$this->renderPartial('userupdate',array('userinfo'=>$one_userinfo,'role_infos'=>$role_infos,'user_id'=>$user_id));
		}
	}
	
	/**
	 * @author xjh
	 * 删除(逻辑删除)
	 */
	
	function actionDel($pages_id){
		
		$menu = Menu::model();
		$row = $menu -> deletePageInfoById($pages_id);
		if($row){
			$this->redirect("./index.php?r=backweb/menu/menu");
		}else{
			echo "<script>alert('删除失败');</script>";
		}
	}
	
	
	
	
}
?>